<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\goods\GoodsAttr;
use common\models\FormVal;

$types = [GoodsAttr::TYPE_SKU => '规格', GoodsAttr::TYPE_SPEC => '扩展属性'];
$inputTypes = [GoodsAttr::INPUT_TYPE_INPUT => '单行输入框', GoodsAttr::INPUT_TYPE_TEXTAREA => '多行文本框', GoodsAttr::INPUT_TYPE_DROPDOWN => '下拉框', GoodsAttr::INPUT_TYPE_CHECKBOX => '复选框'];


/* @var $this yii\web\View */
/* @var $model common\models\GoodsAttr */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="goods-attr-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 30]) ?>
    <?= $form->field($model, 'type')->radioList($types, ['separator' => FormVal::$separator10]); ?>
    
    <div class="is-attr">
        <?= $form->field($model, 'input_type')->radioList($inputTypes, ['separator' => FormVal::$separator10]); ?>
    </div>

    <div class="is-select">
        <?= $form->field($model, 'values')->hint('如果为颜色类型，每一项则使用以下格式：颜色名称|颜色值。 eg：军绿色|#5d762a')->textarea(['rows' => 6]) ?>
    </div>
    
    <div class="is-attr">
        <?= $form->field($model, 'is_not_null')->radioList(FormVal::$bool, ['separator' => FormVal::$separator10]); ?>
    </div>
    
    <div class="is-sku">
        <?= $form->field($model, 'is_aliases')->radioList(FormVal::$bool, ['separator' => FormVal::$separator10]); ?>
        <?= $form->field($model, 'is_upload_img')->radioList(FormVal::$bool, ['separator' => FormVal::$separator10]); ?>
        <?= $form->field($model, 'is_color')->radioList(FormVal::$bool, ['separator' => FormVal::$separator10]); ?>
    </div> 
    
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '新建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>


<script>
    window.onload = function ()
    {
        $('#goodsattr-type input[type=radio]').click(function () {
            var val = $(this).val();
            if (val ==<?= GoodsAttr::TYPE_SKU ?>) {
                $('.is-sku').removeClass('none');
                $('.is-select').removeClass('none');
                $('.is-attr').addClass('none');
            } else {
                $('.is-sku').addClass('none');
                $('.is-select').addClass('none');
                $('.is-attr').removeClass('none');
                <?php if($model->isNewRecord):?>
                    $('#goodsattr-input_type input[type=radio]').attr('checked',false);
                <?php endif;?>
            }
        });

        $('#goodsattr-input_type input[type=radio]').click(function () {
            var val = $(this).val();
            if (val ==<?= GoodsAttr::INPUT_TYPE_CHECKBOX ?> || val ==<?= GoodsAttr::INPUT_TYPE_DROPDOWN ?>) {
                $('.is-select').removeClass('none');
            } else {
                $('.is-select').addClass('none');
            }
        });
        
        <?php if($model->isNewRecord || $model->type==GoodsAttr::TYPE_SKU):?>
            $('#goodsattr-type input[type=radio]')[0].click();
        <?php else:?>
            $('#goodsattr-type input[type=radio]')[1].click();
            <?php if(in_array($model->input_type,[GoodsAttr::INPUT_TYPE_CHECKBOX,GoodsAttr::INPUT_TYPE_DROPDOWN])):?>
                $('.is-select').removeClass('none');
            <?php else:?>
                $('.is-select').addClass('none');
            <?php endif;?>
        <?php endif;?>
    }
</script>